package com.shenyue.nbtownlet.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.util.StorageUtil;
import com.shenyue.nbtownlet.util.StringUtil;

/**
 * 登录管理
 */

public class AppLoginModel {
    private static final String LOGIN_NATIVE = DefaultModel.DEBUG
            ? "http://183.136.157.18:7020/yczjapi/customer/{account}/{pwd}Login_Native"
            : "http://115.238.132.44:81/yczjapi/customer/{account}/{pwd}Login_Native";
    //测试
    //private static final String LOGIN_NATIVE = "http://183.136.157.18:7020/yczjapi/customer/{account}/{pwd}Login_Native""http://115.238.132.44:81/yczjapi/customer/{account}/{pwd}Login_Native";
    //生产
    //private static final String LOGIN_NATIVE = "http://115.238.132.44:81/yczjapi/customer/{account}/{pwd}Login_Native";
    private static final String LOGIN_NATIVE_ACTION = "$login_native$";
    public static final String LOGIN_NATIVE_DATA = "$login_native_data$";
    /**
     * 登录
     */
    public final static String LOGIN_USERNAME = "$login_username$";
    /**
     * 密码
     */
    public final static String LOGIN_PASSWORD = "$login_password$";


    public static void login(final Context context, JSONObject param, final BaseListener listener) {
        String server = LOGIN_NATIVE;
        server = server.replace("{account}", param.getString("Account")).replace("{pwd}", param.getString("Pwd"));
        server += "?chennelID=";
        NbzjClientExManager.instance().post(context, server, LOGIN_NATIVE_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static boolean isLogin(Context context) {
        String val = StorageUtil.getLocalStorage(context, LOGIN_NATIVE_DATA);
        return !StringUtil.isNull(val);
    }

    public static String getIdentityId(Context context) {
        try {
            String val = StorageUtil.getLocalStorage(context, LOGIN_NATIVE_DATA);
            JSONObject data = JSON.parseObject(val);
            //取小城镇用户
            JSONArray IDENTITIES = data.getJSONArray("IDENTITIES");
            for (int i = 0, j = IDENTITIES.size(); i < j; i++) {
                JSONObject item = IDENTITIES.getJSONObject(i);
                if ("36".equals(item.getString("APPID")) && item.getBooleanValue("ISMR")) {
                    return StringUtil.dTs(item.getString("IDENTITYID"));
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    public static String getUserId(Context context) {
        try {
            String val = StorageUtil.getLocalStorage(context, LOGIN_NATIVE_DATA);
            JSONObject data = JSON.parseObject(val);
            return StringUtil.dTs(data.getString("USERID"));
        } catch (Exception e) {
            return null;
        }
    }

    public static String getUserName(Context context) {
        try {
            String val = StorageUtil.getLocalStorage(context, LOGIN_NATIVE_DATA);
            JSONObject data = JSON.parseObject(val);
            JSONObject UserInfo = data.getJSONObject("UserInfo");
            return UserInfo.getString("REALNAME");
        } catch (Exception e) {
            return null;
        }
    }

    public static String getAreaId(Context context) {
        try {
            String val = StorageUtil.getLocalStorage(context, LOGIN_NATIVE_DATA);
            JSONObject data = JSON.parseObject(val);
            //取小城镇用户
            JSONArray IDENTITIES = data.getJSONArray("IDENTITIES");
            for (int i = 0, j = IDENTITIES.size(); i < j; i++) {
                JSONObject item = IDENTITIES.getJSONObject(i);
                if ("36".equals(item.getString("APPID")) && item.getBooleanValue("ISMR")) {
                    return StringUtil.dTs(item.getString("AREAID"));
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
